package com.changyue.util;

import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import com.changyue.MyApplication;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 
 * Title: FileUtils.java
 * Description: 对sd卡的文件相关操作
 * @author Liusong
 * @date 2015-1-12
 * @version V1.0
 */
public class FileOpUtil {

	/**
	 * 判断sdcrad是否已经安装
	 * @return boolean true安装 false 未安装
	 */
	public static boolean isSDCardMounted(){
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}
	
	/**
	 * 得到sdcard的路径
	 * @return
	 */
	public static String getSDCardRoot(){
		System.out.println(isSDCardMounted()+Environment.getExternalStorageState());
		if(isSDCardMounted()){
			return Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		return "";
	}

	/**
	 * 创建文件的路径及文件
	 * @param path 路径，方法中以默认包含了sdcard的路径，path格式是"/path...."
	 * @param filename 文件的名称
	 * @return 返回文件的路径，创建失败的话返回为空
	 */
	public static String createMkdirsAndFiles(String path, String filename) {

		if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
			MyApplication trainSalesApplication = MyApplication.getInstance();
			String newFileRootPath = trainSalesApplication.getExternalFilesDir("log").getAbsolutePath();
			File newFile = new File(newFileRootPath + File.separator + filename);

			if(!newFile.exists()){
				try {
					newFile.createNewFile();
				} catch (IOException e) {
					throw new RuntimeException("创建文件不成功");
				}
			}
			return newFile.getAbsolutePath();
		}

		if (TextUtils.isEmpty(path)) {
			throw new RuntimeException("路径为空");
		}
		path = getSDCardRoot()+ File.separator + path;
		File file = new File(path);
		if (!file.exists()) {
			try {
				file.mkdirs();
			} catch (Exception e) {
				throw new RuntimeException("创建文件夹不成功");
			}
		}

		File f = new File(file, filename);
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				throw new RuntimeException("创建文件不成功");
			}
		}
		return f.getAbsolutePath();
	}
	
	/**
	 * 把内容写入文件
	 * @param path 文件路径
	 * @param text 内容
	 */
	public static void write2File(String path, String text, boolean append){
		BufferedWriter bw = null;
		try {
			//1.创建流对象
			bw = new BufferedWriter(new FileWriter(path,append));
			//2.写入文件
			bw.write(text);
			//换行刷新
			bw.newLine();
			bw.flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4.关闭流资源
			if(bw!= null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 删除文件
	 * @param path
	 * @return
	 */
	public static boolean deleteFile(String path){
		if(TextUtils.isEmpty(path)){
			throw new RuntimeException("路径为空");
		}
		File file = new File(path);
		if(file.exists()){
			try {
				file.delete();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	public static void deleteLogFiles(String path) {
		if (!path.endsWith(File.separator)) {
			path = path + File.separator;
		}

		File file = new File(path);
		if (!file.isDirectory()) {
			try {
				file.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}

		File[] allFiles = file.listFiles();
		if(allFiles == null) {
			return;
		}

		for (File logFile : allFiles) {
			String fileName = logFile.getName();
			try {
				logFile.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static ArrayList<String> iteratorFile(String path) {
		if (!path.endsWith(File.separator)) {
			path = path + File.separator;
		}

		ArrayList<String> ret = new ArrayList<>();
		File file = new File(path);
		if (!file.isDirectory()) {
			ret.add(file.getAbsolutePath());
		}

		File[] allFiles = file.listFiles();
		if(allFiles == null) {
			return ret;
		}

		for (File logFile : allFiles) {
			ret.add(logFile.getAbsolutePath());
		}
		return ret;
	}
}
